package org.dtrd.modules.content.tags.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.content.tags.entity.bean.TagCategoryDetail;
import org.dtrd.modules.content.tags.entity.bean.TagCategoryListInfo;
import org.dtrd.modules.content.tags.entity.po.DtrdExtEmsYwTagCategory;
import org.dtrd.modules.content.tags.entity.request.TagCategoryListRequest;
import org.dtrd.modules.content.tags.entity.request.TagCategoryPageRequest;
import org.dtrd.modules.content.tags.mapper.DtrdExtEmsYwTagCategoryMapper;
import org.dtrd.modules.content.tags.service.IDtrdExtEmsYwTagCategoryService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


/**
 * <p>
 * 标签分类 服务实现类
 * </p>
 *
 * @author QR
 * @since 2022-03-15
 */
@DS("multi-datasource1")
@Service
public class DtrdExtEmsYwTagCategoryServiceImpl extends ServiceImpl<DtrdExtEmsYwTagCategoryMapper, DtrdExtEmsYwTagCategory> implements IDtrdExtEmsYwTagCategoryService {

    @Override
    public List<TagCategoryListInfo> getTagCategoryList(TagCategoryListRequest request) {
        LambdaQueryWrapper<DtrdExtEmsYwTagCategory> queryWrapper = baseQueryWrapper();
        Integer pid = request.getTagCategoryPid();
        if (pid != null) {
            queryWrapper.eq(DtrdExtEmsYwTagCategory::getCategoryPid,pid);
        }
        List<DtrdExtEmsYwTagCategory> poList = list(queryWrapper);
        List<TagCategoryListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(TagCategoryListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public IPage<TagCategoryListInfo> pageTagCategory(TagCategoryPageRequest request) {
        LambdaQueryWrapper<DtrdExtEmsYwTagCategory> queryWrapper = baseQueryWrapper();
        Integer pid = request.getTagCategoryPid();
        if (pid != null) {
            queryWrapper.eq(DtrdExtEmsYwTagCategory::getCategoryPid, pid);
        }
        IPage<DtrdExtEmsYwTagCategory> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = this.page(page, queryWrapper);
        return page.convert(TagCategoryListInfo::new);
    }

    @Override
    public TagCategoryDetail getTagCategory(Integer dataId) {
        DtrdExtEmsYwTagCategory dtrdExtEmsYwTagCategory = getById(dataId);
        return Optional.ofNullable(dtrdExtEmsYwTagCategory)
                        .map(po -> new TagCategoryDetail().parseFromPO(po))
                        .orElse(null);
    }

    @Override
    public Map<Integer, String> getTagCategoryIdToName() {
        HashMap<Integer, String> map = new HashMap<>();
        List<DtrdExtEmsYwTagCategory> tagCategories = list();
        if (CollectionUtil.isNotEmpty(tagCategories)){
            tagCategories.forEach(t->{
                map.put(t.getDataId(), t.getCategoryName());
            });
        }
        return map;
    }

    @Override
    public boolean saveOrUpdateTagCategory(TagCategoryDetail dto) {
        DtrdExtEmsYwTagCategory dtrdExtEmsYwTagCategory = new DtrdExtEmsYwTagCategory();
        if(dto.getDataId() == null){
            dtrdExtEmsYwTagCategory = dtrdExtEmsYwTagCategory.init();
        }
        dtrdExtEmsYwTagCategory.parseFromDTO(dto);
        return saveOrUpdate(dtrdExtEmsYwTagCategory);
    }

    @Override
    public boolean removeTagCategory(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdExtEmsYwTagCategory> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdExtEmsYwTagCategory.class)
                        .eq(DtrdExtEmsYwTagCategory::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdExtEmsYwTagCategory> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdExtEmsYwTagCategory.class)
                        .eq(DtrdExtEmsYwTagCategory::getIsDel, 0);
    }
}
